﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class InventorytypeS : InventorytypeSI
    {
        private readonly WmssystemContext myDb;

        public InventorytypeS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public Task<List<Sys_Inventorytype>> Iqu()
        {
            return myDb.Sys_Inventorytype.Where(i => i.I_TDirection == true).ToListAsync();
        }
        // 出入库类型分页模糊查询
        public Task<List<Sys_Inventorytype>> FenYe(int pageSize, int currentPage, string Key = null, bool? Status = null, bool? Direction = null, string Operator = null)
        {
            var query = myDb.Sys_Inventorytype.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(c => c.I_TTypeCode.Contains(Key) || c.I_TTypeName.Contains(Key));
            }
            if (Status.HasValue)
            {
                query = query.Where(c => c.I_TStatus == Status);
            }
            if (Direction.HasValue)
            {
                query = query.Where(c => c.I_TDirection == Direction);
            }
            if (!string.IsNullOrEmpty(Operator))
            {
                query = query.Where(c => c.I_TOperator.Contains(Operator));
            }
            //筛选逻辑删除
            query = query.Where(c => c.I_TIsDelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //出入库类型添加
        public Task<int> Add(Sys_Inventorytype typeAdd)
        {
            myDb.Sys_Inventorytype.Add(typeAdd);
            return myDb.SaveChangesAsync();
        }
        //出入库类型修改
        public Task<int> Upd(Sys_Inventorytype typeUpd)
        {
            myDb.Sys_Inventorytype.Update(typeUpd);
            return myDb.SaveChangesAsync();
        }
        //出入库类型逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDb.Sys_Inventorytype.FindAsync(id);
            // 更新实体的删除状态
            entity.I_TIsDelete = isDeleted;
            // 标记实体为已修改状态
            myDb.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDb.SaveChangesAsync();
        }

        public Task<List<Sys_Inventorytype>> Iqu1()
        {
            return myDb.Sys_Inventorytype.Where(i => i.I_TDirection == false).ToListAsync();
        }
    }
}
